import smtplib
from email.mime.text import MIMEText
from email.header import Header
from datetime import datetime, timedelta
from flask import Flask, make_response #生成cookie
import base64
import random

def sendMail(receiverEmail,title,content):

    # 邮件信息
    sender_email = '17346885580@163.com'
    receiver_email = receiverEmail  # 将手机号替换为短信提供商的短信转发地址

    # 创建邮件对象
    msg = MIMEText(content)
    msg['From'] = "17346885580@163.com"
    msg['To'] = receiverEmail
    msg['Subject'] = title

    # 连接到 SMTP 服务器
    smtp_server = 'smtp.163.com'
    smtp_port = 465
    smtp_username = "17346885580@163.com"  # 发件人邮箱地址
    smtp_password = 'DZXGNQZPTHBMIIXL'
    smtp_connection = smtplib.SMTP_SSL(smtp_server, smtp_port)
    smtp_connection.login(smtp_username, smtp_password)
    # 发送邮件
    smtp_connection.send_message(msg)
    # 断开连接
    smtp_connection.quit()

def sendCode():
    import re
    #随机数
    code = base64.b64encode(base64.b64encode(base64.b64encode(str(random.randint(100000,999999)).encode("utf-8"))))#生成验证码

    # resp = make_response('Cookie has been set!')
    # expires = datetime.now() + timedelta(seconds==60)  # 设置过期时间60s
    # resp.set_cookie('code', f"{code}", expires=expires)
    with open(r"code.html") as f:
        # sendMail("3217910488@qq.com","云接口注册验证码",f.read().replace("${{code}}",code))
        print(re.sub(r"${{code}}"), code, f.read())
